<?php
namespace app\controllers;

use app\common\corelib\LocalUpload;
use app\logic\ShopLogic;
use app\models\Area;
use yii;

class StoreController extends BackendController
{
    const LIMIT=30;

    public function actionIndex(){
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $shopLogic = new ShopLogic();
        $this->data['breadcrumbs'] = '门店列表管理';
        return $this->render('index', [
            'storelist' => $shopLogic->getStoreList($query, $page),
            'query' => $query
        ]);
    }

    public function actionAddStorelist()
    {
        $shopLogic = new ShopLogic();
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['门店列表管理' => U('store/index'), ' 添加门店列表' => ''];
            return $this->render('add', [
                'theme' => '/assets/',
                'actionUrl' => U('store/add-storelist')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $logicRes = $shopLogic->addStoreList($params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增门店列表成功');
                jump('store/index');
            } else {
                Yii::$app->getSession()->setFlash('error','新增门店列表失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionEditStorelist()
    {
        $shopLogic = new ShopLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('store_list_id');
            $params = Yii::$app->request->get('form');
            if(!$id)
                $id = $params['store_list_id'];
            $page = Yii::$app->request->get('page', 1);
            $store_list = $shopLogic->getStoreListById($id);
            $this->data['breadcrumbs'] = ['门店清单管理' => U('store/index'), $store_list['list_name'] => '', '更新' => ''];

            $store = $shopLogic->getStoreByStoreList($id,$params,$page);
            return $this->render('edit', [
                'storelist'=>$store_list,
                'store'=>$store,
                'theme' => '/assets/',
                'actionUrl' => U('store/edit-storelist'),
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('store_list_id');
            $logicRes = $shopLogic->editStoreListById($id, $params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新清单成功');
                jump('store/index');
            } else {
                Yii::$app->getSession()->setFlash('error','更新清单失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionDelete(){
        $id = Yii::$app->request->get('store_list_id');
        $shopLogic = new ShopLogic();
        $logicRes = $shopLogic->deleteStoreList($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','删除成功');
        } else {
            Yii::$app->getSession()->setFlash('error','删除失败');
        }
        return $this->redirect(U('store/index'));
    }

    public function actionAddStore(){
        $shopLogic = new ShopLogic();
        $id = Yii::$app->request->get('store_list_id');
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['门店清单管理' => U('store/index'),'门店管理' => U(['store/edit-storelist','store_list_id'=>$id]), ' 添加门店' => ''];
            $province = Area::find()->where(['region_level'=>1])->asArray()->all();
            return $this->render('add-store', [
                'province'=>$province,
                'theme' => '/assets/',
                'actionUrl' => U(['store/add-store','store_list_id'=>$id])
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $logicRes = $shopLogic->addStore($id,$params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增门店成功');
                jump(['store/edit-storelist','store_list_id'=>$id]);
            } else {
                Yii::$app->getSession()->setFlash('error','新增门店失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionEditStore()
    {
        $shopLogic = new ShopLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('store_id');
            $store = $shopLogic->getStoreById($id);
            $this->data['breadcrumbs'] = ['门店清单管理' => U('store/index'),'门店管理' => U(['store/edit-storelist','store_list_id'=>$store['store_list_id']]), $store['store_name'] => '', '更新' => ''];
            $province = Area::find()->where(['region_level'=>1])->asArray()->all();
            $city = Area::find()->where(['region_level'=>2,'parent_code'=>$store['province']])->asArray()->all();
            $district = Area::find()->where(['region_level'=>3,'parent_code'=>$store['city']])->asArray()->all();
            return $this->render('edit-store', [
                'province'=>$province,
                'city'=>$city,
                'district'=>$district,
                'store'=>$store,
                'theme' => '/assets/',
                'actionUrl' => U('store/edit-store'),
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('store_id');
            $logicRes = $shopLogic->editStoreById($id, $params);
            $store = $shopLogic->getStoreById($id);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新成功');
                jump(['store/edit-storelist','store_list_id'=>$store['store_list_id']]);
            } else {
                Yii::$app->getSession()->setFlash('error','更新失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionDeleteStore(){
        $id = Yii::$app->request->get('store_id');
        $shopLogic = new ShopLogic();
        $store = $shopLogic->getStoreById($id);
        $logicRes = $shopLogic->deleteStore($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','删除成功');
        } else {
            Yii::$app->getSession()->setFlash('error','删除失败');
        }
        return $this->redirect(U(['store/edit-storelist','store_list_id'=>$store['store_list_id']]));
    }

    public function actionImportStore(){
        $shopLogic = new ShopLogic();
        $id = Yii::$app->request->get('store_list_id');
        $shopLogic = new ShopLogic();
        if($_FILES['store_file']['name']){
            LocalUpload::go_upload_file($_FILES['store_file']);
            $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
            $objPHPExcel = new \PHPExcel();
            $objPHPExcel = \PHPExcel_IOFactory::load(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$uploadresult);
            $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
            foreach($sheetData AS $key=>$value){
                /*if(isset($value['C']))
                    $orderLogic->refundOrder1($value['A'],$value['B'],$value['C']);
                else
                    $orderLogic->refundOrder1($value['A'],$value['B']);*/
                if($key!=1){
                    $province = Area::find()->where(['region_name'=>$value['B']])->asArray()->one();
                    $city = Area::find()->where(['region_name'=>$value['C']])->asArray()->one();
                    $district = Area::find()->where(['region_name'=>$value['D']])->asArray()->one();
                    $params = [
                        'store_name'=>$value['A'],
                        'province'=>$province['region_code'],
                        'city'=>$city['region_code'],
                        'district'=>$district['region_code'],
                        'address'=>$value['E'],
                        'store_pic'=>(isset($value['F']) && $value['F'])?$value['F']:"",
                        'industry'=>(isset($value['G']) && $value['G'])?$value['G']:"",
                        'longitude'=>(isset($value['H']) && $value['H'])?$value['H']:"",
                        'latitude'=>(isset($value['I']) && $value['I'])?$value['I']:"",
                    ];
                    $shopLogic->addStore($id,$params);
                }
            }
        }else{
            $uploadresult = '';
        }
    }


}
